package com.hex.liyue.messagemodule.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.SPUtil;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.messagemodule.adapter.SystemMessageListAdapter;
import com.hex.liyue.messagemodule.bean.SystemMsgResultBean;
import com.hex.liyue.messagemodule.presenter.ISystemMessageView;
import com.hex.liyue.messagemodule.presenter.SystemMessagePresenter;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.utils.EaseCommonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by yukuo on 2016/5/16.
 * 这是一个系统提示消息的界面
 */
public class SystemMessageActivity extends BaseActivity implements View.OnClickListener, ISystemMessageView {
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.ycl_system_message)
    YRecycleview yclSystemMessage;
    private List<SystemMsgResultBean.DataBean> list = new ArrayList<>();
    private SystemMessageListAdapter systemMessageListAdapter;
    private SystemMessagePresenter systemMessagePresenter;
    private String toChatUsername;
    protected Bundle fragmentArgs;

    @Override
    protected void bindPresenter() {
        systemMessagePresenter = new SystemMessagePresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        SPUtil.putString(this,"system","system");
//        fragmentArgs =getIntent().;
//        toChatUsername = fragmentArgs.getString(EaseConstant.EXTRA_USER_ID);
        EMConversation conversation= EMClient.getInstance().chatManager().getConversation("admin", EaseCommonUtils.getConversationType(1), true);
        // 把此会话的未读数置为0
        conversation.markAllMessagesAsRead();
        showLayoutLoading();
        systemMessagePresenter.getDataList(1,10);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        yclSystemMessage.setLayoutManager(linearLayoutManager);
        yclSystemMessage.setRefreshAndLoadMoreListener(new YRecycleview.RefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                systemMessagePresenter.getDataList(1,10);
            }

            @Override
            public void onLoadMore() {
                systemMessagePresenter.getDataList(1,10);
            }
        });
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_system_message;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("系统消息");
    }

    @Override
    @OnClick({R.id.iv_toobar_back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void showLayoutLoading() {
        showLoad(this, "加载中");
    }

    @Override
    public void showLayoutContent() {
        closeLoad();
        yclSystemMessage.loadMoreComplete();
        yclSystemMessage.setReFreshComplete();
    }

    @Override
    public void setAdapter(SystemMsgResultBean systemMsgResultBean) {
        list.clear();
        if (systemMsgResultBean != null) {
            for (SystemMsgResultBean.DataBean dataBean : systemMsgResultBean.data) {
                list.add(dataBean);
            }
        }
        if (systemMessageListAdapter == null) {
            systemMessageListAdapter = new SystemMessageListAdapter(list,SystemMessageActivity.this);
            yclSystemMessage.setAdapter(systemMessageListAdapter);
        }
        systemMessageListAdapter.notifyDataSetChanged();
    }

    @Override
    public void showLod(String msg) {
        showLoad(SystemMessageActivity.this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }
}
